<?php

namespace JLPay\Services\Clearing;

use JLPay\Services\BaseService;
use JLPay\Models\Clearing\WithdrawQueryRequest;
use JLPay\Models\Clearing\WithdrawQueryResponse;
use JLPay\Models\Clearing\WithdrawApplyRequest;
use JLPay\Models\Clearing\WithdrawApplyResponse;
use JLPay\Models\Clearing\FileUploadRequest;
use JLPay\Models\Clearing\FileUploadResponse;
use JLPay\Models\Clearing\AccBalanceRequest;
use JLPay\Models\Clearing\AccBalanceResponse;
use JLPay\Models\Clearing\NotifyDto;

/**
 * 清算服务类
 */
class ClearingService extends BaseService
{
    /**
     * 构造函数
     *
     * @param \JLPay\Core\OrgConfig|null $config
     */
    public function __construct($config = null)
    {
        if ($config !== null) {
            parent::__construct($config);
        } else {
            // 如果config为null，不调用父类构造函数，只支持本地方法
            $this->config = null;
        }
    }

    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     * @throws \InvalidArgumentException
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        if ($this->config === null) {
            throw new \InvalidArgumentException("ClearingService未初始化配置，无法进行HTTP请求");
        }

        $baseUrl = $this->config->getUrl();

        if ($apiPath) {
            return $baseUrl . $apiPath;
        }

        // 使用请求类中定义的API_PREFIX和API_URI
        if (defined($requestClass . '::API_PREFIX') && defined($requestClass . '::API_URI')) {
            $apiPrefix = constant($requestClass . '::API_PREFIX');
            $apiUri = constant($requestClass . '::API_URI');
            
            // 组合API路径
            $path = ltrim($apiPrefix, '/') . '/' . ltrim($apiUri, '/');
            return $baseUrl . '/' . $path;
        }

        // 如果没有定义API_URI，使用默认路径
        return $baseUrl . '/fund/withdraw';
    }

    /**
     * 查询提现结果
     *
     * @param WithdrawQueryRequest $request 请求对象
     * @return WithdrawQueryResponse 响应对象
     */
    public function queryWithdraw(WithdrawQueryRequest $request): WithdrawQueryResponse
    {
        return $this->post($request, WithdrawQueryResponse::class);
    }

    /**
     * 申请提现
     *
     * @param WithdrawApplyRequest $request 请求对象
     * @return WithdrawApplyResponse 响应对象
     */
    public function applyWithdraw(WithdrawApplyRequest $request): WithdrawApplyResponse
    {
        return $this->post($request, WithdrawApplyResponse::class);
    }

    /**
     * 上传视频文件
     *
     * @param FileUploadRequest $request 请求对象
     * @return FileUploadResponse 响应对象
     */
    public function uploadVideo(FileUploadRequest $request): FileUploadResponse
    {
        return $this->post($request, FileUploadResponse::class);
    }

    /**
     * 查询结算资金
     *
     * @param AccBalanceRequest $request 请求对象
     * @return AccBalanceResponse 响应对象
     */
    public function queryAccBalance(AccBalanceRequest $request): AccBalanceResponse
    {
        return $this->post($request, AccBalanceResponse::class);
    }

    /**
     * 解析通知消息
     *
     * @param array $notifyData 通知数据
     * @return NotifyDto 通知DTO对象
     */
    public function parseNotify(array $notifyData): NotifyDto
    {
        return new NotifyDto($notifyData);
    }
} 